package com.alipay.servlet.auth;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayOpenPublicGisQueryModel;
import com.alipay.api.request.AlipayOpenPublicContactFollowBatchqueryRequest;
import com.alipay.api.request.AlipayOpenPublicFollowBatchqueryRequest;
import com.alipay.api.request.AlipayOpenPublicGisQueryRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipayOpenPublicContactFollowBatchqueryResponse;
import com.alipay.api.response.AlipayOpenPublicFollowBatchqueryResponse;
import com.alipay.api.response.AlipayOpenPublicGisQueryResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.factory.AlipayAPIClientFactory;
import com.alipay.util.RequestUtil;

/**
 * 获取关注用户相关信息接口调用示例 (测试通过)
 * 
 * @author liliang
 *
 */
public class AlipayPublicFollowUser {

	
	// 获取服务窗关注者列表
	public static void followlistQuery() {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayOpenPublicFollowBatchqueryRequest request = new AlipayOpenPublicFollowBatchqueryRequest();
		request.setBizContent("{}");
		AlipayOpenPublicFollowBatchqueryResponse response = null;
		try {
			response = alipayClient.execute(request);
			System.out.println(response.getBody());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	// 获取用户地理位置
	public static void gisQuery() {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayOpenPublicGisQueryRequest request = new AlipayOpenPublicGisQueryRequest();
		AlipayOpenPublicGisQueryModel model = new AlipayOpenPublicGisQueryModel();
		model.setUserId("2088702468804908");
		request.setBizModel(model);
		AlipayOpenPublicGisQueryResponse response = null;
		try {
			response = alipayClient.execute(request);
			System.out.println(response.getBody());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	

	public static void main(String[] args) {
		followlistQuery();
	}


	// 获取用户 授权后的信息
	public static void getUserOauthInfo() {
		try {
			AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
			AlipaySystemOauthTokenRequest oauthTokenRequest = new AlipaySystemOauthTokenRequest();
			oauthTokenRequest.setCode("498a8d2dd2e74824afd5eeeca0faVX90");
			oauthTokenRequest.setGrantType("authorization_code");

			AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(oauthTokenRequest);
			System.out.println(oauthTokenResponse.getAccessToken());
			String accessToken = oauthTokenResponse.getAccessToken();

			// 通过accessToken获取用户信息
			AlipayUserInfoShareRequest shareRequest = new AlipayUserInfoShareRequest();
			AlipayUserInfoShareResponse userInfoShareResponse = alipayClient.execute(shareRequest, accessToken);
			if (userInfoShareResponse.isSuccess()) {
				System.out.println("调用成功");
				String responseBody = userInfoShareResponse.getBody();
				System.out.println("授权成功后获取的用户信息====>" + responseBody);
			} else {
				System.out.println("调用失败");
			}

			System.out.println("=============================================================================");
			
			/*
			AlipayOpenPublicContactFollowBatchqueryRequest contactFollowBatchqueryRequest = new AlipayOpenPublicContactFollowBatchqueryRequest();
			AlipayOpenPublicContactFollowBatchqueryResponse contactFollowBatchqueryResponse = alipayClient.execute(contactFollowBatchqueryRequest,
					accessToken);
			if (contactFollowBatchqueryResponse.isSuccess()) {
				System.out.println("调用成功");
				String responseBody = contactFollowBatchqueryResponse.getBody();
				System.out.println("查询服务窗联系人关注列表====>" + responseBody);
			} else {
				System.out.println("调用失败");
			}
*/
		} catch (AlipayApiException e) {
			// 处理异常
			e.printStackTrace();
		}
	}
}
